package com.ruoyi.quartz.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.quartz.domain.*;
import com.ruoyi.quartz.mapper.SyncDataMapper;
import com.ruoyi.quartz.mapper.SyncDataErrSendMapper;
import com.ruoyi.quartz.task.SyncData;
import com.ruoyi.quartz.task.SyncData2;
import com.ruoyi.quartz.util.MyResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("ZJController")
@CrossOrigin(origins = {"http://172.23.99.19","http://localhost","http://172.20.238.100"}) //  允许 进行跨域请求
public class ZJController {

    @Autowired
    private SyncData syncData;
    @Autowired
    private SyncData2 syncData2;
    @Autowired
    private SyncDataErrSendMapper syncDataMapper2;
    @Autowired
    private SyncDataMapper syncDataMapper;

    @RequestMapping("uploadZjData")
    public MyResponse uploadZjData(WeaveReport weaveReport) {
        boolean empty = ObjectUtils.isEmpty(weaveReport);
        if (empty) {
            return MyResponse.fail("WeaveReport 传递的参数需要是整经对象 并且不得为空");
        }
        syncData.sysncZjWeavereport2Mes(weaveReport);
        return MyResponse.success("已提交到上传程序", "");
    }

    @RequestMapping("uploadEveryData")
    public MyResponse uploadEveryData(String billNo, String methodName, String errMsgTblInstid) {

        if (StringUtils.isEmpty(billNo) || StringUtils.isEmpty(methodName) || StringUtils.isEmpty(errMsgTblInstid)) {
            return MyResponse.fail("单据号 或者方法名 序列号不得为空");
        }

        JSONObject jsonObject1 = new JSONObject();
        if (methodName.equals("sysncZjWeavereport2Mes")) {//整经
            WeaveReport zj = new WeaveReport();
            zj.setSerialNo(billNo);
            zj.setOrgId("66");
            jsonObject1 = syncData2.sysncZjWeavereport2Mes(zj);
        }
        if (methodName.equals("syncJsWeavereport2Mes")) {//浆纱
            WeaveJs weaveJs = new WeaveJs();
            weaveJs.setOrgId("66");
            weaveJs.setSerialNo(billNo);
            jsonObject1 = syncData2.syncJsWeavereport2Mes(weaveJs);
        }

        if (methodName.equals("syncSzWeavereport2Mes")) {//上轴
            WeaveSz weaveSz = new WeaveSz();
            weaveSz.setSerialNo(billNo);
            weaveSz.setOrgId("66");
            jsonObject1 = syncData2.syncSzWeavereport2Mes(weaveSz);
        }
        if (methodName.equals("syncKzWeavereport2Mes")) {//开织
            WeaveKz weaveKz = new WeaveKz();
            weaveKz.setSerialNo(billNo);
            weaveKz.setOrgId("66");
            jsonObject1 = syncData2.syncKzWeavereport2Mes(weaveKz);
        }
        if (methodName.equals("syncXjWeavereport2Mes")) {//下机
            WeaveXj weaveXj = new WeaveXj();
            weaveXj.setSerialNo(billNo);
            weaveXj.setOrgId("66");
            jsonObject1 = syncData2.syncXjWeavereport2Mes(weaveXj);
        }
        if (methodName.equals("syncCzWeavereport2Mes")) {//穿综
            WeaveCz weaveCz = new WeaveCz();
            weaveCz.setSerialNo(billNo);
            weaveCz.setOrgId("66");
            jsonObject1 = syncData2.syncCzWeavereport2Mes(weaveCz);
        }
        if (methodName.equals("syncLbWeavereport2Mes")) {//落布
            WeaveLb weaveLb = new WeaveLb();
            weaveLb.setSerialNo(billNo);
            weaveLb.setOrgId("66");
            jsonObject1 = syncData2.syncLbWeavereport2Mes(weaveLb);
        }

        SendMessageErr sendMessageErr = syncDataMapper2.getOneSendMessageErr(errMsgTblInstid);
        if (jsonObject1.isEmpty() || sendMessageErr==null){
            return MyResponse.fail("单据号不存在 处理程序为空");
        }
        /*{"state":true,"message":"操作成功","value":[]}*/
        sendMessageErr.setBillNo(billNo);
        sendMessageErr.setStatus(jsonObject1.get("state").toString());
        sendMessageErr.setMaintenanceDate(new Date());
        sendMessageErr.setInstId(Long.parseLong(errMsgTblInstid));
        sendMessageErr.setErrMessage(jsonObject1.get("message").toString());
        int i = syncDataMapper2.updateSendMessageErr(sendMessageErr);
        if(Convert.toBool(jsonObject1.get("state"))){
            return  MyResponse.success("上传成功", i);
        }else{
            return  MyResponse.fail("上传异常");
        }
    }

    @RequestMapping("getEveryData")
    public MyResponse getEveryData(String sendMessageErr,
                                   @RequestParam(defaultValue = "0") int pageSize,
                                   @RequestParam(defaultValue = "10") int pageCount) {
        SendMessageErr bean = JSONUtil.toBean(sendMessageErr, SendMessageErr.class);
        List<SendMessageErr> everyData1 = syncDataMapper2.getEveryData(bean);

        PageHelper.startPage(pageSize,pageCount);
        List<SendMessageErr> everyData = syncDataMapper2.getEveryData(bean);
        return MyResponse.success(everyData1.size()+"",everyData);
    }
}
